import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {PpypService} from '../../ppyp.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {CommonService} from '../../../../services-common/common.service';
import {NzModalService} from 'ng-zorro-antd/modal';
import {UsedStorePService} from '../../../used-store/used-store-p.service';

interface TABLE {
  query: any;
  listData: any;
}

enum ORDERSTATUS {
  '有效',
  '无效',
  '待付款',
  '待审核',
  '审核不通过',
  '审核通过',
  '排队发货',
  '待发货',
  '待收货（已发货）',
  '交易完成',
  '支付超时',
  '客服取消',
  '客户取消',
  '售后中',
  '全部退货',
  '交易关闭' = 16
}

enum REFUNDSTATE {
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  '部分退款',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  // @ts-ignore
  '-',
  '全部退款'
}

enum ADJUSTTYPE {
  '普通商品',
  '二手商品'
}

@Component({
  selector: 'app-ppyp-order-manage',
  templateUrl: './ppyp-order-manage.component.html',
  styleUrls: ['./ppyp-order-manage.component.scss']
})
export class PpypOrderManageComponent implements OnInit {
  ORDERSTATUS = ORDERSTATUS;
  REFUNDSTATE = REFUNDSTATE;
  ADJUSTTYPE = ADJUSTTYPE;
  table: TABLE = {
    query: {
      orderNum: '',
      goods: '',
      user: '',
      source: [],
      expressNo: '',
      status: [],
      refundState: [],
      creatTime: null,
      payTime: null,
      deliverTime: null,
      page: 1,
      pageSize: 20
    },
    listData: {
      page: 1,
      pageSize: 20,
      total: 100,
      records: []
    }
  };
  tableExpandSet = new Set<number>();
  // 表格选中状态
  tableChecked = false;
  // 表格是否有选中的
  tableIndeterminate = false;
  cancelModal = {
    visible: false,
    remark: ''
  };
  logistics = [];

  constructor(
    public router: Router,
    public ppypService: PpypService,
    private nzMessageService: NzMessageService,
    private commonService: CommonService,
    private nzModalService: NzModalService,
    private usedStorePService: UsedStorePService,
  ) {
  }

  ngOnInit(): void {
    this.getTableList();
    this.usedStorePService.dictData('logistics_company').then((res: any) => {
      if (res.code === 0) {
        this.logistics = res.data.list.records;
      }
    });
  }

  onExpandChange(id: number, checked: boolean): void {
    if (checked) {
      this.tableExpandSet.add(id);
    } else {
      this.tableExpandSet.delete(id);
    }
  }

  onPageIndexChange(index: number) {
    this.table.query.page = index;
    this.getTableList();
  }

  onPageSizeChange(pageSize: number) {
    this.table.query.pageSize = pageSize;
    this.getTableList();
  }

  queryBtn() {
    this.table.query.page = 1;
    this.getTableList();
  }

  resetCondition() {
    this.table.query = {
      orderNum: '',
      goods: '',
      user: '',
      source: [],
      expressNo: '',
      status: [],
      refundState: [],
      creatTime: null,
      payTime: null,
      deliverTime: null,
      page: 1,
      pageSize: 20
    };
    this.queryBtn();
  }

  // 获取展示区域内容列表
  getTableList() {
    // this.table.query.page = 8;
    this.table.listData.records = [];
    this.ppypService.getOrderList(
      {
        goods: this.table.query.goods,
        orderNum: this.table.query.orderNum.trim(),
        user: this.table.query.user,
        source: this.table.query.source.join(','),
        expressNo: this.table.query.expressNo,
        status: this.table.query.status.join(','),
        refundState: this.table.query.refundState.join(','),
        orderBeginTime: this.ppypService.formatTime(this.table.query.creatTime ? this.table.query.creatTime[0] : '', 'date'),
        orderEndTime: this.ppypService.formatTime(this.table.query.creatTime ? this.table.query.creatTime[1] : '', 'date'),
        paymentBeginTime: this.ppypService.formatTime(this.table.query.payTime ? this.table.query.payTime[0] : '', 'date'),
        paymentEndTime: this.ppypService.formatTime(this.table.query.payTime ? this.table.query.payTime[1] : '', 'date'),
        shipBeginTime: this.ppypService.formatTime(this.table.query.deliverTime ? this.table.query.deliverTime[0] : '', 'date'),
        shipEndTime: this.ppypService.formatTime(this.table.query.deliverTime ? this.table.query.deliverTime[1] : '', 'date'),
        page: this.table.query.page,
        pageSize: this.table.query.pageSize,
      }
    ).then((res: any) => {
      if (res.code === 0) {
        this.table.listData = res.data;
        this.table.listData.records.forEach((i: any) => {
          if (i.oidList) {
            i.oidList.forEach((o: any) => {
              if (o.orderDetailsList.some((ii: any) => ii.source === 1) && o.orderDetailsList.some((ii: any) => ii.source === 0)) {
                o.source = 3;
              }
            });
          }
          if (i.orderDetailsList) {
            if (i.orderDetailsList.some((ii: any) => ii.source === 1) && i.orderDetailsList.some((ii: any) => ii.source === 0)) {
              i.source = 3;
            }
          }
        });
      }
    });
  }

  onTableChecked(event: boolean) {
    console.log(event);
  }

  rowIndeterminate(data: any) {
    return false;
  }

  onRowChecked($event: boolean) {

  }

  onItemChecked(id: any, ev: boolean) {

  }

  toDetail(id: any) {
    this.router.navigate(['PPYP/ppypOrderDetail'], {queryParams: {id}});
  }

  showCancelModal(data: any) {
    console.log(data);
    this.cancelModal.visible = true;
  }

  hideCancelModal() {
    this.cancelModal.visible = false;
  }

  cancelOrder() {
    this.cancelModal.visible = false;
  }

  export() {

    this.nzModalService.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的订单数据，为了导出速度请尽可能控制时间范围。确定导出？',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        const params = {
          goods: this.table.query.goods,
          orderNum: this.table.query.orderNum,
          user: this.table.query.user,
          source: this.table.query.source.join(','),
          expressNo: this.table.query.expressNo,
          status: this.table.query.status.join(','),
          refundState: this.table.query.refundState.join(','),
          orderBeginTime: this.ppypService.formatTime(this.table.query.creatTime ? this.table.query.creatTime[0] : '', 'date'),
          orderEndTime: this.ppypService.formatTime(this.table.query.creatTime ? this.table.query.creatTime[1] : '', 'date'),
          paymentBeginTime: this.ppypService.formatTime(this.table.query.payTime ? this.table.query.payTime[0] : '', 'date'),
          paymentEndTime: this.ppypService.formatTime(this.table.query.payTime ? this.table.query.payTime[1] : '', 'date'),
          shipBeginTime: this.ppypService.formatTime(this.table.query.deliverTime ? this.table.query.deliverTime[0] : '', 'date'),
          shipEndTime: this.ppypService.formatTime(this.table.query.deliverTime ? this.table.query.deliverTime[1] : '', 'date'),
        };
        const res: any = await this.ppypService.exportGoodsData(params);
        if (res.type === 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '订单信息');
        } else {
          this.nzMessageService.error('导出失败');
        }
      },
      nzCancelText: '关闭'
    });
  }

  getLogisticsName(content: any) {
    if (content) {
      return this.logistics.filter((i: any) => i.content === content)[0]['name'];
    } else {
      return '';
    }
  }
}
